package com.utils;


import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.ArrayUtils;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.*;
@Slf4j
public class TxtFileReadUtils {


    public  static Map<String,List<Map<String, Object>>> getInfo(String parantPath){
        File fiels=new File(parantPath);
        final File[] txts = fiels.listFiles(pathname -> pathname.getName().contains("txt"));
        Map<String,List<Map<String, Object>>> returnMap=new HashMap<>();
        for (File txt : txts) {
            log.info("开始读取文件{}",txt.getName());
            // 使用随机读取
            RandomAccessFile fileRead = null;
            StringBuilder sbr=new StringBuilder();

            try {
                //使用读模式
                String readLineString = "";
                fileRead = new RandomAccessFile(txt, "r");
                fileRead.seek(0);
                List<Map<String, Object>> infoList=new ArrayList<>();
                List<String> colunmList=new ArrayList<>();
                while ((readLineString = fileRead.readLine()) != null) {
                    String lineInfo = new String(readLineString.getBytes("ISO-8859-1"), "utf-8");
                    String[] split = lineInfo.split("\\|~\\|");
                    if(lineInfo.endsWith("\\|~\\|")){

                    }
                    if(colunmList.isEmpty()){
                        colunmList.addAll(Arrays.asList(split));
                    }else{
//                        List<String> readLineList=new ArrayList<>();
//                        readLineList.addAll(Arrays.asList(split));
//                        infoList.add(readLineList);
                        Map<String,Object> map=new HashMap<>();
                        for (int i = 0; i < colunmList.size(); i++) {
                              int length = split.length;

                            if(i<length) {
                                String value = split[i];
                                if (value.contains("\\")) {
                                    value=value.replace("\\","\\\\");
                                }
                                if (value.contains("\"")) {
                                    value=value.replace("\"","\\\"");
                                }

                                map.put(colunmList.get(i), value);
                            }else{
                                map.put(colunmList.get(i),null) ;
                            }
                        }
                        infoList.add(map);
                    }

                }

                returnMap.put(txt.getName().contains("2019")?txt.getName().substring(0,txt.getName().indexOf("2019")):txt.getName(),infoList);

            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (fileRead != null) {
                    try {
                        //关闭资源
                        fileRead.close();
                    } catch (Exception e) {
                    }
                }
            }
        }
        return  returnMap;
    }

    public static void main(String[] args) {
//        String parantPath="D:\\工作文档\\sinobase\\需求文档\\呼叫中心\\ATLAS";
//          Map<String, List<List<String>>> info = getInfo(parantPath);
//          log.info(info.size()+"--");
    }
}
